<template>
    <div class="upload-container">
        <el-button icon='upload' :style="{background:color,borderColor:color}" @click=" dialogVisible=true" type="primary">上传图片
        </el-button>
        <el-dialog v-model="dialogVisible">
            <el-upload
                class="editor-slide-upload"
                :action="FileUploadUrl"
                :multiple="false"
                :file-list="fileList"
                :show-file-list="true"
                list-type="picture-card"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
                :before-upload="beforeUpload">
                <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </el-dialog>
    </div>
</template>
<script>
// 文件上传测试地址 http://123.206.189.55:28810/cos/upload
// 文件上传的正式服地址　http://dfs.upload1.jzexueyun.com/cos/upload
export default {
  // name: 'editorSlideUpload',
  props: {
    color: {
      type: String,
      default: '#20a0ff'
    }
  },
  data() {
    return {
      dialogVisible: false,
      listObj: {},
      fileList: [],
       FileUploadUrl : process.env.UPLOAD_URL,
    }
  },
  methods: {
    checkAllSuccess() {
      return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map(v => this.listObj[v])
      if (!this.checkAllSuccess()) {
        this.$message('正在上传，请稍后再试！')
        return
      }
      this.$emit('successCBK', arr)
      this.listObj = {}
      this.fileList = []
      this.dialogVisible = false
    },
    handleSuccess(response, file) {
      console.log(1212);
      const uid = file.uid;
      const objKeyArr = Object.keys(this.listObj); 
      for (var i = 0, len = objKeyArr.length; i < len; i++) {
        console.log(this.listObj[objKeyArr[i]].uid, uid);
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.content.accessUrl;
          this.listObj[objKeyArr[i]].hasSuccess = true;
          return
        }
      }
    },
    handleRemove(file) {
      if (file) {
        const uid = file.uid
        const objKeyArr = Object.keys(this.listObj)
        for (let i = 0, len = objKeyArr.length; i < len; i++) {
          if (this.listObj[objKeyArr[i]].uid === uid) {
            delete this.listObj[objKeyArr[i]]
            return
          }
        }
      }
    },
    // 获取文件的大小
    _getFileSize(size) {			
      return Math.floor(size/1048576);
    },
    beforeUpload(file) {
      
      const imgMax = 204800 ;
      // console.log(imgMax,file.size)

      const isJPG = (file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg');
      if (!isJPG) {
          this.$message.error('上传图片支持 JPG、jpeg、png 格式!');
          return false;
      }
      if(file.size > imgMax){
        this.$message.error(`"${file.name}" 该图片过大，请上传小于200K的图片`);
        return false;
      }
      const _self = this
      const _URL = window.URL || window.webkitURL
      const fileName = file.uid
      this.listObj[fileName] = {}
      
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = _URL.createObjectURL(file)
        img.onload = function() {
          _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
        }
        resolve(true)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
    .upload-container {
        .editor-slide-upload {
            margin-bottom: 20px;
        }
    }
</style>
